use ndarray::prelude::*;

pub fn global2local(key: &ArrayView1<usize>, query: &ArrayView1<usize>) -> Array1<usize> {
    let max_key = key.iter().max().unwrap();
    let mut temp = Array1::<usize>::zeros(max_key + 1);
    for (i, &k) in key.iter().enumerate() {
        temp[k] = i;
    }
    query.mapv(|q| temp[q])
}

mod tests {
    use super::*;

    #[test]
    fn global2local_works() {
        let k = array![0, 1, 2, 4, 5];
        let q = array![0, 2, 4];
        assert_eq!(global2local(&k.view(), &q.view()), array![0, 2, 3]);
    }
}
